package com.hancai.config.handler;

import com.hancai.result.CodeEnum;
import com.hancai.result.R;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 涵菜
 * @version 1.0
 * springMVC:统一异常处理类，controller发生了异常，统一用该类进行处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    //父类异常处理
    @ExceptionHandler(value = Exception.class)
    public R handException(Exception e){
        e.printStackTrace();
        return R.FAIL(e.getMessage());
    }

    @ExceptionHandler(value = DataAccessException.class)
    public R handException(DataAccessException e){
        e.printStackTrace();
        return R.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
    }
}
